package com.ruoyi.door.controller.admin.repair;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.util.ObjectUtil;
import com.easy.query.api.proxy.client.EasyEntityQuery;
import com.easy.query.core.api.pagination.EasyPageResult;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.door.controller.admin.repair.vo.*;
import com.ruoyi.door.domain.DoorRepairOrderLog;
import com.ruoyi.door.service.DoorRepairOrderLogService;
import com.ruoyi.door.service.DoorRepairOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.door.domain.DoorRepairOrder;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 报修工单Controller
 *
 * @author ruoyi
 * @date 2025-07-29
 */
@Api(value = "报修工单Controller", tags = "报修工单Controller")

@RestController
@RequestMapping("/admin-api/door/order/repair")
public class DoorRepairOrderController extends BaseController {
    @Autowired
    private DoorRepairOrderService doorRepairOrderService;
    @Autowired
    private DoorRepairOrderLogService doorRepairOrderLogService;
    @Autowired
    private EasyEntityQuery easyEntityQuery;
    /**
     * 查询报修工单列表
     */
    @ApiOperation("查询报修工单列表")
    @GetMapping("/list")
    public TableDataInfo list(DoorRepairOrderPageReqVO doorRepairOrder) {
        return doorRepairOrderService.selectDoorRepairOrderPageList(doorRepairOrder,null);
    }
    @ApiOperation("导出报修工单列表")
    @GetMapping("/export")
    public void export(DoorRepairOrderPageReqVO doorRepairOrder,HttpServletResponse response) {
        List<DoorRepairOrderRespVO> list = doorRepairOrderService.selectDoorRepairOrderList(doorRepairOrder,null);
        List<DoorRepairOrderExportRespVO> doorRepairOrderExportRespVOS = BeanUtil.copyToList(list, DoorRepairOrderExportRespVO.class);

        ExcelUtil<DoorRepairOrderExportRespVO> util = new ExcelUtil<DoorRepairOrderExportRespVO>(DoorRepairOrderExportRespVO.class);
        util.exportExcel(response, doorRepairOrderExportRespVOS, "报修数据");
    }

    /**
     * 获取报修工单详细信息
     */
    @ApiOperation("获取报修工单详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        DoorRepairOrder doorRepairOrder = doorRepairOrderService.selectDoorRepairOrderById(id);
        DoorRepairOrderRespVO doorRepairOrderRespVO = BeanUtil.copyProperties(doorRepairOrder, DoorRepairOrderRespVO.class);
        List<DoorRepairOrderLog> doorRepairOrderLogs = doorRepairOrderLogService.selectListByRepairOrderId(id);
        if (ObjectUtil.isNotNull(doorRepairOrderRespVO)) {
            doorRepairOrderLogs.forEach(x -> {
                SysUser sysUser = easyEntityQuery.queryable(SysUser.class).where(s -> s.userId().eq(Long.valueOf(x.getCreateBy()))).firstOrNull();
                x.setCreateByNickName(Opt.ofNullable(sysUser).map(SysUser::getNickName).orElse(""));
            });
            doorRepairOrderRespVO.setLogs(doorRepairOrderLogs);
        }
        return success(doorRepairOrderRespVO);
    }

    /**
     * 新增报修工单
     */
    @ApiOperation("新增报修工单")
    @Log(title = "报修工单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody DoorRepairOrderCreateReqVO doorRepairOrder) {
        return toAjax(doorRepairOrderService.insertDoorRepairOrder(doorRepairOrder, SecurityUtils.getUserId(), "2") > 0);
    }
    @ApiOperation("新增报修工单H5")
    @Log(title = "报修工单", businessType = BusinessType.INSERT)
    @PostMapping("/addByH5")
    public AjaxResult addByH5(@RequestBody DoorRepairOrderCreateReqVO doorRepairOrder) {
        return toAjax(doorRepairOrderService.insertDoorRepairOrder(doorRepairOrder, SecurityUtils.getAppUserId(), "1") > 0);
    }
    /**
     * 修改报修工单
     */
    @ApiOperation("修改工单")
    @Log(title = "报修工单", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody DoorRepairOrderUpdateReqVO req) {
        return toAjax(doorRepairOrderService.updateDoorRepairOrder(req) > 0);
    }

    /**
     * 修改报修工单
     */
    @ApiOperation("指派工单")
    @PostMapping("/assign")
    public AjaxResult assign(@RequestBody DoorRepairOrderAssignReqVO doorRepairOrder) {
        return toAjax(doorRepairOrderService.assign(doorRepairOrder, SecurityUtils.getUserId(), "2") > 0);
    }



    @ApiOperation("运维人员我的订单列表")
    @GetMapping("/listByOpera")
    public TableDataInfo listByOpera(DoorRepairOrderPageReqVO doorRepairOrder) {

       return doorRepairOrderService.selectDoorRepairOrderPageList(doorRepairOrder,SecurityUtils.getUserId());

    }

    @ApiOperation("接收订单")
    @PostMapping("/receive")
    public AjaxResult receive(@RequestBody DoorRepairOrderReceiveReqVO reqVO) {
        return toAjax(doorRepairOrderService.receive(reqVO, SecurityUtils.getUserId(), "2") > 0);
    }

    @ApiOperation("完成订单")
    @PostMapping("/complete")
    public AjaxResult updateByOpera(@RequestBody DoorRepairOrderCompleteReqVO reqVO) {
        return toAjax(doorRepairOrderService.complete(reqVO, SecurityUtils.getUserId(), "2") > 0);
    }
    @ApiOperation("删除订单")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody DoorRepairOrderCompleteReqVO reqVO) {
        return toAjax(doorRepairOrderService.delete(reqVO) > 0);
    }
}
